home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / OS / FWMemory / Include / FWMemorH.h < prev    next >
Encoding:
Text File  |  1994-04-21  |  6.4 KB  |  222 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWMemorH.h
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Creation Date:        3/28/94
  7. //
  8. //    Copyright:    © 1993, 1994 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #ifndef FWMEMORH_H
  13. #define FWMEMORH_H
  14.  
  15. #ifndef FWPLATME_H
  16. #include <FWPlatMe.h>
  17. #endif
  18.  
  19. #ifndef __STDDEF__
  20. #include <stddef.h>
  21. #endif
  22.  
  23. #ifndef __TYPES__
  24. #include <Types.h>
  25. #endif
  26.  
  27.  
  28. //========================================================================================
  29. // Forward class declarations
  30. //========================================================================================
  31.  
  32. class FW_CMemoryHookList;
  33.  
  34.  
  35. //========================================================================================
  36. // Type definitions
  37. //========================================================================================
  38.  
  39. typedef unsigned long FW_BytePtr;
  40. typedef unsigned long FW_BlockSize;
  41.  
  42. #ifdef FW_DEBUG
  43. //========================================================================================
  44. // FW_CMemoryHook (FW_DEBUG only)
  45. //
  46. //        A Memory hook that can be used to track heap operations by subclassing FW_CMemoryHook
  47. //        and overriding the methods of interest. Your subclass is then registered with a
  48. //        FW_CMemoryHeap.
  49. //
  50. //========================================================================================
  51.  
  52. class FW_CMemoryHook
  53. {
  54. public:
  55.     // These methods should all be abstract, but FW_CMemoryHookList instantiates a
  56.     // FW_CMemoryHook to use as the head of its linked list, so here we just use empty
  57.     // inlines. A FW_CMemoryHook still cannot be instantiated because the constructor
  58.     // is protected.
  59.  
  60.     virtual FW_BlockSize AboutToAllocate(FW_BlockSize size);
  61.     virtual void* DidAllocate(void* blk, FW_BlockSize);
  62.     virtual void* AboutToBlockSize(void* blk);
  63.     virtual void* AboutToFree(void* blk);
  64.     virtual void AboutToRealloc(void*& , FW_BlockSize&);
  65.     virtual void* DidRealloc(void* blk, FW_BlockSize);
  66.     virtual void AboutToReset();
  67.     virtual void Comment(const char*);
  68.  
  69.     virtual~ FW_CMemoryHook();
  70.  
  71. protected:
  72.     FW_CMemoryHook();
  73.  
  74. private:
  75.     FW_CMemoryHook* fNextHook, * fPreviousHook;
  76.  
  77.     friend FW_CMemoryHookList;
  78.     
  79.     FW_CMemoryHook(const FW_CMemoryHook& blk);
  80.     FW_CMemoryHook& operator=(const FW_CMemoryHook& blk);
  81.         // This class shouldn't be copied.
  82. };
  83. #endif
  84.  
  85.  
  86. #ifdef FW_DEBUG
  87. //========================================================================================
  88. // FW_CMemoryHookList (FW_DEBUG only)
  89. //
  90. //        A list of memory hooks. This is a special linked list that assumes the links for
  91. //        the list are fields of FW_CMemoryHook. This avoids endless recursion were we to
  92. //        allocate nodes for the MemoryHooks here.
  93. //
  94. //========================================================================================
  95.  
  96. class FW_CMemoryHookList
  97. {
  98. public:
  99.     FW_CMemoryHookList();
  100.  
  101.     void Add(FW_CMemoryHook* aMemoryHook);
  102.     void Remove(FW_CMemoryHook* aMemoryHook);
  103.     FW_CMemoryHook* First();
  104.     FW_CMemoryHook* Next();
  105.     FW_CMemoryHook* Previous();
  106.     FW_CMemoryHook* Last();
  107.  
  108.     ~FW_CMemoryHookList();
  109.  
  110. private:
  111.     FW_CMemoryHook fHead;
  112.     FW_CMemoryHook* fCurrentHook;
  113.     
  114.     FW_CMemoryHookList(const FW_CMemoryHookList& blk);
  115.     FW_CMemoryHookList& operator=(const FW_CMemoryHookList& blk);
  116.         // This class shouldn't be copied.
  117. };
  118. #endif
  119.  
  120.  
  121. //========================================================================================
  122. // FW_CMemoryHeap
  123. //
  124. //        Abstract base class for memory heaps.
  125. //
  126. //========================================================================================
  127.  
  128. class FW_CMemoryHeap
  129. {
  130. public:
  131.     enum { kBlockTypeId = 0 };
  132.  
  133.     static FW_CMemoryHeap* fHeapList;
  134.     static FW_CMemoryHeap* GetFirstHeap();
  135.  
  136.     void* Allocate(FW_BlockSize size);
  137.     FW_BlockSize BlockSize(const void* block) const;
  138.     virtual unsigned long BytesAllocated() const;
  139.     virtual unsigned long BytesFree() const = 0;
  140.     void Free(void*);
  141.     virtual FW_CMemoryHeap* GetNextHeap() const;
  142.     virtual Boolean GetZapOnAllocate() const;
  143.     virtual Boolean GetZapOnFree() const;
  144.     virtual unsigned long HeapSize() const = 0;
  145.     virtual unsigned long NumberAllocatedBlocks() const;
  146.     void Reset();
  147.     void* Reallocate(void* block, FW_BlockSize newSize);
  148.     virtual void SetZapOnAllocate(Boolean = false);
  149.     virtual void SetZapOnFree(Boolean = false);
  150.  
  151.     void* operator new(SIZE_T size);
  152.     void operator delete(void* ptr);
  153.  
  154.     virtual~ FW_CMemoryHeap();
  155.  
  156. #ifdef FW_DEBUG
  157.     static const char* kDefaultDescription;
  158.     virtual const char* GetDescription() const;
  159.     virtual void SetDescription(const char* description = kDefaultDescription);
  160.  
  161.     virtual void InstallHook(FW_CMemoryHook* FW_CMemoryHook);
  162.     virtual void RemoveHook(FW_CMemoryHook* FW_CMemoryHook);
  163.  
  164.     virtual Boolean GetAutoValidation() const;
  165.     virtual void SetAutoValidation(Boolean = false);
  166.     Boolean IsValidBlock(void* blk) const;
  167.     virtual Boolean IsMyBlock(void* blk) const = 0;
  168.  
  169.     virtual void Check() const = 0;
  170.     virtual void Print(char* msg = "") const = 0;
  171. #endif
  172.  
  173. protected:
  174.     FW_CMemoryHeap(Boolean autoValidation = false,
  175.                    Boolean zapOnAllocate = true,
  176.                    Boolean zapOnFree = false);
  177.  
  178.     virtual void* AllocateRawMemory(FW_BlockSize size);
  179.     virtual void* DoAllocate(FW_BlockSize size, FW_BlockSize& allocatedSize) = 0;
  180.     virtual FW_BlockSize DoBlockSize(const void* block) const = 0;
  181.     virtual void DoFree(void*) = 0;
  182.     virtual void DoReset() = 0;
  183.     virtual void* DoReallocate(void* block, FW_BlockSize newSize, FW_BlockSize& allocatedSize);
  184.     virtual void FreeRawMemory(void* ptr);
  185.  
  186. #ifdef FW_DEBUG
  187.     virtual Boolean DoIsValidBlock(void* blk) const = 0;
  188.     virtual void CompilerCheck();
  189. #endif
  190.  
  191. private:
  192.     FW_CMemoryHeap* fNextHeap;
  193.     Boolean fZapOnAllocate;
  194.     Boolean fZapOnFree;
  195.     Boolean fAutoValidation;
  196.     unsigned long fBytesAllocated;
  197.     unsigned long fNumberAllocatedBlocks;
  198.  
  199. #ifdef FW_DEBUG
  200.     enum { kDescriptionLength = 32 };
  201.     char fDescription[kDescriptionLength];
  202.     FW_CMemoryHookList fMemoryHookList;
  203.  
  204.     FW_BlockSize CallAboutToAllocateHooks(FW_BlockSize size);
  205.     void* CallDidAllocateHooks(void* blk, FW_BlockSize size);
  206.     void* CallAboutToFW_BlockSizeHooks(void* blk);
  207.     void* CallAboutToFreeHooks(void* blk);
  208.     void CallAboutToReallocHooks(void*& blk, FW_BlockSize& size);
  209.     void* CallDidReallocHooks(void* blk, FW_BlockSize size);
  210.     void CallAboutToResetHooks();
  211.     void CallCommentHooks(const char* comment);
  212.     void ValidateAndReport(void* blk) const;
  213. #endif
  214.     
  215.     FW_CMemoryHeap(const FW_CMemoryHeap& blk);
  216.     FW_CMemoryHeap& operator=(const FW_CMemoryHeap& blk);
  217.         // This class shouldn't be copied.
  218. };
  219.  
  220.  
  221. #endif
  222.